// pages/part2/multiple.js
const db=wx.cloud.database()
const question_db=db.collection('question')
Page({
    data: {
      total: 0,
      answer: "A",
      current: 1, //从第一题开始
      isSelect: false,
      questionResult: [],
      titles:[],
      tk:[]
    },
  
  
    onLoad() {
      
      var version=wx.getStorageSync('bb')
      question_db.where({"_id":version})
    .get()
    .then(res=>{
      console.log(res.data[0].question[0]['tiankong'])
      this.setData({
        titles:res.data[0].question[0]['tiankong']
      })
      var subject = this.data.titles[0]
      console.log("subject", subject)
      this.setData({ 
        subject: subject,
        total: this.data.titles.length
      })
      var that=this
      that.setData({
        id:that.options.id,
      })
      if(that.data.id){
        that.setData({
          current:parseInt(that.data.id),
          subject:this.data.titles[that.data.id-1],
        })
        var multiple=wx.getStorageSync('tk')
      console.log(multiple)
      this.setData({
        tkong:multiple[this.data.current-1]
      })
      }
  })
      var multiple=wx.getStorageSync('tk')
      console.log(multiple)
      this.setData({
        tkong:multiple[this.data.current-1]
      })
    },
    onPageScroll: function (e) {
      console.log(e)
      if (e.scrollTop > 100) {
       this.setData({
        floorstatus: true
       });
      } else {
       this.setData({
        floorstatus: false
       });
      }
     },
    next(){
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
         scrollTop: 0
        })
       } 
      this.setData({
       tkong:''
      })
      if (this.data.current < this.data.total) {
        this.setData({
          current: this.data.current + 1,
          subject: this.data.titles[this.data.current],
          // userxz:this.data.getQR[this.data.current].select
        })
        
      
      var multiple=wx.getStorageSync('tk')
      console.log(multiple)
      this.setData({
        tkong:multiple[this.data.current-1]
      })
    }
      else if(this.data.current == this.data.total){
        wx.redirectTo({
          url: '../part2/review',
        })
      }
      
    },
    last(){
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
         scrollTop: 0
        })
       } 
      this.setData({
        tkong:''
       })
        if (this.data.current > 1) {
          this.setData({
            current: this.data.current -1,
            
          })
          this.setData({
            subject: this.data.titles[this.data.current-1],
          })
        
        var multiple=wx.getStorageSync('tk')
      console.log(multiple)
      this.setData({
        tkong:multiple[this.data.current-1]
      })}
      else if (this.data.current == 1){
        wx.redirectTo({
          url: '../part2/review',
        })
      }
    },
    shuru(e){
      let nr = e.detail.value
      this.data.tk[this.data.current-1]=nr
      console.log(this.data.tk)
      wx.setStorageSync('tk', this.data.tk)
     
      
    },
    
    submit(e) {
      if(!this.data.getQR){
      console.log(e)
      let userSelcet = e.target.dataset.select
      if (userSelcet !== undefined) {
        this.data.questionResult.push({
          "questionId": this.data.current,
          "select": userSelcet
        }) // 记录
        // 不是最后一题，current+1,并进行下一题
    
       
      
      //  this.setData({
      //    userxz:getQR[this.data.current]
      //  })
        if (this.data.current < this.data.total) {
          this.setData({
            current: this.data.current + 1,
            subject: this.data.titles[this.data.current]
          })
        }
        // 如果已经到了最后一题
        if (this.data.current === this.data.total) {
          wx.showToast({
            icon: 'none',
            title: '已经最后一道题啦',
          })
        }
        // 答完最后一道题
        if (this.data.current === this.data.total && this.data.questionResult.length === this.data.total) {
          console.log("恭喜你全部答完")
          console.log(this.data.questionResult)
          wx.navigateTo({
            url: '../part2/review',
          })
          wx.setStorageSync('QR', this.data.questionResult)
        }
      }
    }
    else{
  
      console.log(e)
      let userSelcet = e.target.dataset.select
      if (userSelcet !== undefined) {
        this.data.getQR[this.data.current-1]={
          "questionId": this.data.current,
          "select": userSelcet
        }
        console.log(this.data.getQR)
        wx.setStorageSync('QR', this.data.getQR)
        var getQR=wx.getStorageSync('QR')
        this.setData({
          getQR:getQR,
          userxz:this.data.getQR[this.data.current].select
        })
        if (this.data.current < this.data.total) {
          this.setData({
            current: this.data.current + 1,
            subject: this.data.titles[this.data.current]
          })
        }
      }
    }
    },
  
  })